//
//  XFWelcomeViewController.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/13.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "XFWelcomeViewController.h"
#import "XFUserAccountModel.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface XFWelcomeViewController ()

@property(nonatomic, weak) UIImageView *backView;
@property(nonatomic, weak) UIImageView *iconView;
@property(nonatomic, weak) UILabel *nameLable;
@end

@implementation XFWelcomeViewController

- (void)loadView {

  UIImageView *backView = [[UIImageView alloc] init];
  backView.image = [UIImage imageNamed:@"ad_background"];
  self.view = backView;
  self.backView = backView;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  [self setupUI];
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  [self startAnimation];
}

- (void)startAnimation {

  [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(self.view.mas_bottom).offset(-kScreenH + 180);
  }];

  [UIView animateWithDuration:1.2
      delay:0
      usingSpringWithDamping:0.9
      initialSpringVelocity:9.8
      options:0
      animations:^{
        //
        [self.view layoutIfNeeded];
      }
      completion:^(BOOL finished) {

        [UIView animateWithDuration:1
            animations:^{
              self.nameLable.alpha = 1;
            }
            completion:^(BOOL finished) {
              //跳转页面
              [[NSNotificationCenter defaultCenter]
                  postNotificationName:@"selectViewcontroller"
                                object:nil];
            }];

      }];
}

- (void)setupUI {
  XFUserAccountModel *userAccount = [[XFUserAccountModel alloc] init];
  UIImageView *iconView = [[UIImageView alloc] init];
  iconView.layer.cornerRadius = 45;
  iconView.layer.masksToBounds = YES;
  [self.view addSubview:iconView];
  self.iconView = iconView;

  UILabel *nameLable = [[UILabel alloc]
      initWithTitle:[NSString stringWithFormat:@"%@ 欢迎回来", userAccount.name]
              Color:[UIColor lightGrayColor]
               Font:19
             Margin:0];
  nameLable.alpha = 0;
  [self.view addSubview:nameLable];
  self.nameLable = nameLable;

  //布局
  [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {

    make.centerX.equalTo(self.view.mas_centerX);
    make.bottom.equalTo(self.view.mas_bottom).offset(-180);
    make.width.equalTo(@90);
    make.height.equalTo(@90);
  }];

  [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.iconView.mas_centerX);
    make.top.equalTo(self.iconView.mas_bottom).offset(20);
  }];

  [self.iconView sd_setImageWithURL:[NSURL URLWithString:userAccount.avatar_url]
                   placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
}

@end
